package com.fable.web.controller.customer;

import com.fable.biz.operate.CustomerService;
import com.fable.biz.operate.CustomerSourceService;
import com.fable.biz.operate.CustomerTypeService;
import com.fable.biz.operate.dto.CustomerDTO;
import com.fable.biz.operate.dto.CustomerSourceDTO;
import com.fable.biz.operate.dto.CustomerTypeDTO;
import com.fable.biz.operate.enums.CustomerStatusEnum;
import com.fable.biz.operate.query.CustomerQueryCondition;
import com.fable.common.config.RequireLogin;
import com.fable.common.enums.BusiErrCode;
import com.fable.common.utils.JsonResult;
import com.fable.dal.mapper.ext.CountryExtMapper;
import com.fable.dal.mapper.ext.ProductExtMapper;
import com.fable.dal.model.*;
import com.fable.utils.StringUtil;
import com.fable.web.common.admin.UserTools;
import com.fable.web.controller.base.BaseController;
import com.fable.web.page.P;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by weishuai on 18/4/7.
 */
@Controller
@RequireLogin
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    @Autowired
    CustomerService customerService;

    @Autowired
    CustomerSourceService customerSourceService;

    @Autowired
    CustomerTypeService customerTypeService;

    @Autowired
    CountryExtMapper countryExtMapper;

    @Autowired
    ProductExtMapper productExtMapper;

    @Value("${sum}")
    private int priCustomerSum;

    /**
     * 跳转到公海列表
     *
     * @param modelMap
     * @param condition
     * @return
     */
    @RequestMapping("/pubCustomerList.htm")
    public String pubCustomerList(ModelMap modelMap, CustomerQueryCondition condition,HttpSession session) {
        condition.setStatus(CustomerStatusEnum.gonghai.getStatus());
        List<CustomerDTO> result = customerService.findCustomerList(condition);
        modelMap.put("customerDTOs", result);
        modelMap.put("query", condition);
        return P.Common.pubCustomerList;
    }

    /**
     * 跳转到私海列表
     *
     * @param modelMap
     * @param condition
     * @return
     */
    @RequestMapping("/priCustomerList")
    public String priCustomerList(ModelMap modelMap, CustomerQueryCondition condition, HttpSession session) {
        //status 私海客户
        condition.setStatus(CustomerStatusEnum.sihai.getStatus());
        Long loginId = UserTools.getLoginId(session);
        condition.setTheirBy(String.valueOf(loginId));
        List<CustomerDTO> result = customerService.findCustomerList(condition);
        modelMap.put("customerDTOs", result);
        modelMap.put("query", condition);
        return P.Common.priCustomerList;
    }

    /**
     * 跳转到服务中客户
     *
     * @param modelMap
     * @param condition
     * @return
     */
    @RequestMapping("/serviceCustomerList.htm")
    public String serviceCustomerList(ModelMap modelMap, CustomerQueryCondition condition, HttpSession session) {
        //status 服务中状态
        condition.setStatus(CustomerStatusEnum.ser.getStatus());
        Long loginId = UserTools.getLoginId(session);
        condition.setTheirBy(String.valueOf(loginId));
        List<CustomerDTO> result = customerService.findCustomerList(condition);
        modelMap.put("customerDTOs", result);
        modelMap.put("query", condition);
        return P.Common.serCustomerList;
    }


    /**
     * 跳转公海客户页面
     *
     * @return
     */
    @RequestMapping("/pubCustomerAdd.htm")
    public String pubCustomerAddHtm(ModelMap modelMap, Integer id, HttpSession session) {
        if (id != null && id != 0) {
            CustomerQueryCondition condition = new CustomerQueryCondition();
            condition.setId(id);
            condition.setStatus(CustomerStatusEnum.gonghai.getStatus());
            List<CustomerDTO> customerDTO = customerService.findCustomerList(condition);
            modelMap.put("customer", customerDTO.get(0));
        }
        List<CustomerSourceDTO> customerSourceDTOS = customerSourceService.querySourceAll(null);
        List<CustomerTypeDTO> customerTypeDTOS = customerTypeService.queryCustomerTypeList(null);
        List<CountryDO> countryList = countryExtMapper.selectByQuery(null);
        List<ProductDO> productList = productExtMapper.selectByQuery(null);
        modelMap.put("sourceResult", customerSourceDTOS);
        modelMap.put("typeResult", customerTypeDTOS);
        modelMap.put("country", countryList);
        modelMap.put("product", productList);
        return P.Common.pubCustomerAdd;
    }

    /**
     * 添加公海客户
     *
     * @param customerDTO
     * @param session
     * @return
     */
    @RequestMapping("/pubCustomerAdd.json")
    @ResponseBody
    public JsonResult pubCustomerAdd(@ModelAttribute CustomerDTO customerDTO, HttpSession session) {
        JsonResult jsonResult = new JsonResult();
        if (StringUtil.isBlank(customerDTO.getName())
                || StringUtil.isBlank(customerDTO.getCompanyName())
                || customerDTO.getCustomerSourceId() == 0
                || customerDTO.getCustomerTypeId() == 0) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
        }
        //公海客户状态
        customerDTO.setStatus(CustomerStatusEnum.gonghai.getStatus());
        Long loginId = UserTools.getLoginId(session);
        customerDTO.setTheirBy(String.valueOf(loginId));
        int count = customerService.addOrModifyCustomer(customerDTO);
        if (count == 0) {
            jsonResult.setErrCode(BusiErrCode.INNER_ERROR);
        }
        return jsonResult;
    }

    /**
     * 跳到私海客户页面
     *
     * @return
     */
    @RequestMapping("/priCustomerAdd.htm")
    public String priCustomerAddHtm(ModelMap modelMap, Integer id) {
        if (id != null && id != 0) {
            CustomerQueryCondition condition = new CustomerQueryCondition();
            condition.setId(id);
            condition.setStatus(CustomerStatusEnum.sihai.getStatus());
            List<CustomerDTO> customerDTO = customerService.findCustomerList(condition);
            modelMap.put("customer", customerDTO.get(0));
        }
        List<CustomerSourceDTO> customerSourceDTOS = customerSourceService.querySourceAll(null);
        List<CustomerTypeDTO> customerTypeDTOS = customerTypeService.queryCustomerTypeList(null);
        List<CountryDO> countryList = countryExtMapper.selectByQuery(null);
        List<ProductDO> productList = productExtMapper.selectByQuery(null);
        modelMap.put("sourceResult", customerSourceDTOS);
        modelMap.put("typeResult", customerTypeDTOS);
        modelMap.put("country", countryList);
        modelMap.put("product", productList);
        return P.Common.priCustomerAdd;
    }

    /**
     * 私海客户添加
     *
     * @param customerDTO
     * @param session
     * @return
     */
    @RequestMapping("/priCustomerAdd.json")
    @ResponseBody
    public JsonResult priCustomerAdd(@ModelAttribute CustomerDTO customerDTO, HttpSession session) {
        JsonResult jsonResult = new JsonResult();
        if (StringUtil.isBlank(customerDTO.getName())
                || StringUtil.isBlank(customerDTO.getCompanyName())
                || customerDTO.getCustomerSourceId() == 0
                || customerDTO.getCustomerTypeId() == 0) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
        }
        //私海客户状态
        customerDTO.setStatus(CustomerStatusEnum.sihai.getStatus());
        Long loginId = UserTools.getLoginId(session);
        customerDTO.setTheirBy(String.valueOf(loginId));
        int count = customerService.addOrModifyCustomer(customerDTO);
        if (count == 0) {
            jsonResult.setErrCode(BusiErrCode.INNER_ERROR);
        }
        return jsonResult;
    }

    /**
     * 修改客户状态
     *
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/modifyCustomerStatus.json")
    @ResponseBody
    public JsonResult modifyCustomerStatus(String id, String status, HttpSession session) {
        JsonResult jsonResult = new JsonResult();
        if (StringUtil.isBlank(id) || StringUtil.isBlank(status)) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
        }
        if (status.equals(CustomerStatusEnum.gonghai.getStatus())) {
            Long loginId = UserTools.getLoginId(session);
            if (customerService.priPriCustomer(String.valueOf(loginId)) > priCustomerSum) {
                jsonResult.setErrCode(BusiErrCode.PRI_CUSTOMER_SUM);
            }
        }
        CustomerDTO customerDTO = new CustomerDTO();
        customerDTO.setId(Integer.valueOf(id));
        customerDTO.setStatus(Integer.valueOf(status));
        int count = customerService.addOrModifyCustomer(customerDTO);
        if (count == 0) {
            jsonResult.setErrCode(BusiErrCode.INNER_ERROR);
        }
        return jsonResult;
    }


    /**
     * 逻辑删除用户成功
     *
     * @return
     */
    @RequestMapping("/delCustomer.json")
    @ResponseBody
    public JsonResult delCustomer(String id) {
        JsonResult jsonResult = new JsonResult();
        if (StringUtil.isEmail(id)) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
            return jsonResult;
        }
        CustomerDTO customerDTO = new CustomerDTO();
        customerDTO.setId(Integer.valueOf(id));
        customerDTO.setStatus(CustomerStatusEnum.delete.getStatus());
        customerDTO.setGmtModified(new Date());
        int count = customerService.delCustomer(customerDTO);
        if (count != 1) {
            jsonResult.setErrCode(BusiErrCode.INNER_ERROR);
        }
        return jsonResult;
    }


    /**
     * 跳转到客户相信页面
     *
     * @param modelMap
     * @param id
     * @return
     */
    @RequestMapping("/detailCustomer.htm")
    public String detailCustomer(ModelMap modelMap, Integer id) {
        if (id != null && id != 0) {
            CustomerDO customerDO = customerService.getById(id);
            if (customerDO.getCustomerSourceId() != null) {
                CustomerSourceDO customerSource = customerSourceService.querySourceById(customerDO.getCustomerSourceId());
                modelMap.put("customerSource", customerSource);
            }
            if (customerDO.getCustomerTypeId() != null) {
                CustomerTypeDO customerType = customerTypeService.queryCustomerById(customerDO.getCustomerTypeId());
                modelMap.put("customerType", customerType);
            }
            if (customerDO.getCustomerTypeId() != null) {
                CountryDO country = countryExtMapper.selectByPrimaryKey(customerDO.getCountryId());
                modelMap.put("country", country);
            }
            if (customerDO.getProductId() != null) {
                ProductDO product = productExtMapper.selectByPrimaryKey(customerDO.getProductId());
                modelMap.put("product", product);
            }
            modelMap.put("customer", customerDO);
        }
        return P.Common.customerDetail;
    }
}
